
#include "xapp_config.h"

#include <os_msg.h>
#include <os_task.h>
#include <os_sync.h>
//#include <hub_task.h>
#include <app_msg.h>
#include "trace.h"
#include "string.h"


#include "xtask+peripheral.h"
#include "xtask+app.h"

#include "xapp_ble.h"

#if USER_CHECK_ENABLED( PEDOMETER)
#include "xapp_pedometer.h"
#endif 

#if USER_CHECK_ENABLED( HRS)
#include "xapp_hrs.h"
#endif

#if USER_CHECK_ENABLED( ECG)
#include "xapp_ecg.h"
#endif

#if USER_CHECK_ENABLED( TOUCHPANEL)
#include "xapp_touchpanel.h"
#include "em_touch_panel.h"
#endif 

#if USER_CHECK_ENABLED( TEMPERATURE )
#include "xapp_temperature.h"
#endif 

//#include "em_buttons.h"
//#include "em_accelerator.h"

#include "xDLPS.h"

#include "gui.h"

#include "log.h"

#define LOG0     LOG_INFO0
#define LOG1     LOG_INFO1

#define xTASK_PERI_PRIORITY     1
void *peri_task_handle;
void *peri_msg_queue_handle; 

static void xtask_peripheral_handle_msg( msg_t* msg ) ;
void xtask_peripheral_handler(void * pvParameter);

// create this task before app task 
void xtask_peripheral_create(void)
{
    os_task_create( 
                    &peri_task_handle, 
                    "peri task", 
                    xtask_peripheral_handler, 
                    NULL, 
                    256 * 8, 
                    xTASK_PERI_PRIORITY 
    ) ;
    
}

bool xtask_peripheral_send_msg(msg_t* msg)
{
    if( os_msg_send( peri_msg_queue_handle, msg, 0 ) == false )
    {
        LOG0("hub peri send msg fail");
        return false;
    }
    
    return true;
}
bool xtask_peripheral_send_msg_from_ISR(msg_t* msg)
{
    if( os_msg_send( peri_msg_queue_handle, msg, 0 ) == false )
    {
        LOG0("hub peri send msg from ISR fail");
        return false;
    }
    
    return true;
}


void xtask_peripheral_handler(void * pvParameter)
{
    msg_t msg;
//    peri_msg_queue_handle = xQueueCreate( 8, sizeof(msg) );
    os_msg_queue_create( &peri_msg_queue_handle, 16, sizeof(msg_t) );
    if( peri_msg_queue_handle != NULL ) 
    {
        LOG1("1peri_msg_queue_handle = 0x%X", (uint32_t)peri_msg_queue_handle );
    }
    
    LOG0("enter PERIPHERAL task LOOP");
    while( 1 ) 
    {
        if( os_msg_recv( peri_msg_queue_handle, &msg, 0xFFFFFFFF ) == true )
        {
            xtask_peripheral_handle_msg( &msg ) ;
        }
    }
}

/*
-Gsensor init/sleep/work/stop
-Heartrate init/sleep/work
-others sensor init/sleep/work
-BLE data process 
*/
static void xtask_peripheral_handle_msg( msg_t* p_msg ) 
{
    switch( p_msg->type ) 
    {
#if USER_CHECK_ENABLED( PEDOMETER )
        case MSG_TYPE_G_SENSOR:
        xapp_pedometer_msg_handler( p_msg );
        break;
#endif 
        
#if USER_CHECK_ENABLED( HRS )
        case MSG_TYPE_HRS:
        xapp_hrs_msg_handler( p_msg );
        break;
#endif 
        
#if USER_CHECK_ENABLED( ECG )
        case MSG_TYPE_ECG:
       // xapp_ecg_msg_handler( p_msg );
        break;
#endif 
        
#if USER_CHECK_ENABLED( TOUCHPANEL)        
        case MSG_TYPE_TOUCHPANEL:
        xapp_touchpanel_msg_handler( p_msg );        
        break;
#endif 
        
        
#if USER_CHECK_ENABLED( TEMPERATURE )
        case MSG_TYPE_TEMPERATURE:
        xapp_temperature_msg_handler( p_msg );
        break;
#endif 
                
        case MSG_TYPE_BLE:        
        if( MSG_BLE_TYPE_RX_DATA == p_msg->sub_type )
        {
            xapp_ble_rx_data_process();
        }
        break;
    }
}


